package org.example.controller;


import jakarta.servlet.http.HttpServletRequest;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/user")
public class UserController {

    @GetMapping("/info")
    public String info(HttpServletRequest request,
                       @RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient client,
                       @AuthenticationPrincipal OAuth2User user) {
        request.setAttribute("userName", user.getName());
        request.setAttribute("clientName", client.getClientRegistration().getClientName());
        request.setAttribute("userAttributes", user.getAttributes());

        return "userinfo"; //返回用户信息
    }


}
